package cn.fclub.print
{
	import flash.events.EventDispatcher;
	
	import mx.controls.Text;
	import mx.printing.FlexPrintJob;
	import mx.printing.FlexPrintJobScaleType;
	
	import spark.components.Group;
	
	import cn.fclub.error.FclubFileException;
	import cn.fclub.event.PrintEvent;
	import cn.fclub.print.invoice.Express;
	
	public class InvoicePrinter extends EventDispatcher //extends Application
	{
		private var _basicGroup:Group = null;
		private var _printJob:FlexPrintJob = null;
		private var _logText:Text = null;
		private var _express:Express = null;
		private var _data:XML = null;
		private static const _SYSTEM_EXPRESS_NAME:String = 'configName';
		
		public function InvoicePrinter():void
		{
			super();
		}
		private function log(s:String):void
		{
			trace(s);
			logText.text += "\n" + s;
		}
		
		public function initialize(pj:FlexPrintJob,bg:Group,text:Text):void
		{
			basicGroup = bg;
			printJob = pj;
			logText = text;
		}

		
		/**
		 * Public Method
		 * 
		 * xmlStr格式如下：
		 * <data>
		 * 	<express>FILEPATH of a specified express layout.</express>
		 * </data>
		 */
		public function doPrint( xmlStr :String ): void
		{
			log( 'come to print' );
			if( xmlStr.length == 0 ) return;
			 data = new XML( xmlStr );
			
			express = new Express(this);
			
			try {
				log( ' express url is:' + data.@express.toString()+'# sysConfig='+data.@[SYSTEM_EXPRESS_NAME] +':'+data.@[SYSTEM_EXPRESS_NAME].toString().length);
				if( data.@[SYSTEM_EXPRESS_NAME].toString().length >1 ){
					express.useSysConfig( data.@[SYSTEM_EXPRESS_NAME] );
					print();
				}
				else express.initLayoutData( data.@express.toString() );
			}
			catch( e:FclubFileException ){
				log( "Node EXPRESS is not a right XML file:"+e.message );
				return;
			}catch( e:Error )
			{
				log ( e.message );
				return;
			}
		}
		public function print():void{
			log( 'start print' );
			// start print code
			printJob.start();
			
			// basic Group
			var tmpGroup:Group = null;
			log( 'before add print bitmap' );
			// add print bitmap			
			for each( var xmlItem:XML in data.children() )
			{
//				trace(xmlItem);
				tmpGroup = express.buildInvoiceData( xmlItem );
				basicGroup.addElement( tmpGroup );
				printJob.addObject( tmpGroup, FlexPrintJobScaleType.NONE );
			}
			// print
			printJob.send();
			
			// finish print
			finishPrint();
		}
		public function finishPrint():void
		{
			var ev:PrintEvent = new PrintEvent(PrintEvent.AFTER_PRINT);
			dispatchEvent(ev);
		}
		public function get express():Express
		{
			return _express;
		}
		
		public function set express(value:Express):void
		{
			_express = value;
		}
		
		public function get data():XML
		{
			return _data;
		}
		
		public function set data(value:XML):void
		{
			_data = value;
		}
		public function get basicGroup():Group
		{
			return _basicGroup;
		}

		public function set basicGroup(value:Group):void
		{
			_basicGroup = value;
		}

		public function get printJob():FlexPrintJob
		{
			return _printJob;
		}

		public function set printJob(value:FlexPrintJob):void
		{
			_printJob = value;
		}

		public function get logText():Text
		{
			return _logText;
		}

		public function set logText(value:Text):void
		{
			_logText = value;
		}

		public static function get SYSTEM_EXPRESS_NAME():String
		{
			return _SYSTEM_EXPRESS_NAME;
		}

		
	}
}